<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" title="排程" width="950px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <div class="formTopFont">生产排程单</div>
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="8">
            <el-form-item label="排程单号" prop="scheduleNo">
              <el-input v-model="ruleForm.scheduleNo" :disabled="detailInfo.status !== 1" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="作业单号" prop="reportNo">
              <el-input v-model="ruleForm.reportNo" :disabled="detailInfo.status !== 1" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计划单号" prop="planNo">
              <el-input v-model="detailInfo.planNo" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="detailInfo.materialName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物料编码" prop="materialCode">
              <el-input v-model="detailInfo.materialCode" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排程数量" prop="scheduleNum">
              <el-input v-model="detailInfo.scheduleNum" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="物料Bom" prop="bomId">
              <el-select v-model="ruleForm.bomId" style="width: 100%" :disabled="detailInfo.status !== 1" filterable>
                <el-option v-for="item in materialBomList" :key="item.id" :label="item.bomVersion" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排程开始时间" prop="scheduleBeginDate">
              <el-date-picker v-model="ruleForm.scheduleBeginDate" :disabled="detailInfo.status !== 1" type="date" placeholder="选择日期时间" style="width: 100%" format="yyyy-MM-dd" value-format="yyyy-MM-dd" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排程结束时间" prop="scheduleEndDate">
              <el-date-picker v-model="ruleForm.scheduleEndDate" :disabled="detailInfo.status !== 1" type="date" placeholder="选择日期时间" style="width: 100%" format="yyyy-MM-dd" value-format="yyyy-MM-dd" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="排程人" prop="scheduleBy">
              <el-select v-model="ruleForm.scheduleBy" style="width: 100%" :disabled="detailInfo.status !== 1" filterable>
                <el-option v-for="item in userAllList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="作业车间" prop="factoryId">
              <tree-select ref="treeSelect" style="width: 100%" :disabled="detailInfo.status !== 1" :props="treeProps" :options="factoryData" :accordion="true" :value="ruleForm.factoryId" search-key="factoryName" @getNode="getValue($event)" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="ruleForm.remark" type="textarea" rows="3" :disabled="detailInfo.status !== 1" maxlength="500" show-word-limit />
        </el-form-item>
      </el-form>
    </el-row>
    <p style="text-align:right;">
      <el-button type="primary" v-if="detailInfo.status === 1" icon="el-icon-check" @click="submitFormSave('ruleForm')">排程</el-button>
    </p>
    <bottom-font ref="bottomFont" :obj-name="ruleForm.scheduleName ? ruleForm.scheduleName : this.$store.getters.compInfo.userName" :obj-date="ruleForm.createAt" />
  </el-dialog>
</template>
<script>
import TreeSelect from '@/components/TreeSelect'
import bottomFont from '@/components/BottomFont'
export default {
  components: { TreeSelect, bottomFont },
  props: {
    userAllList: {
      type: Array,
      default: () => {
        return []
      }
    },
    factoryData: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    const checkScheduleNo = async (rule, value, callback) => {
      if (this.ruleForm.id) {
        const res = await this.$axios.get(
          `/pro/schedule/verifyNoUnique?no=${value}&id=${
            this.ruleForm.id || ''
          }`
        )
        if (res?.data.code === 200) {
          return res.data.data
            ? callback()
            : new Error(callback('排程单号重复！'))
        } else {
          return new Error(callback(res.data.msg))
        }
      }
    }
    const checkReportNo = async (rule, value, callback) => {
      if (this.ruleForm.id) {
        const res = await this.$axios.get(
          `/pro/schedule/verifyReportNoUnique?reportNo=${value}&id=${
            this.ruleForm.id || ''
          }`
        )
        if (res?.data.code === 200) {
          return res.data.data
            ? callback()
            : new Error(callback('作业单号重复！'))
        } else {
          return new Error(callback(res.data.msg))
        }
      }
    }
    return {
      title: '',
      ruleForm: {
        factoryId: '',
        reportNo: '',
        scheduleNo: '',
        scheduleBeginDate: '',
        scheduleEndDate: '',
        bomId: '',
        scheduleBy: null,
        scheduleNum: '',
        remark: ''
      },
      rules: {
        scheduleNo: [
          { required: true, message: '排程单号不能为空!', trigger: 'change' },
          { validator: checkScheduleNo, trigger: 'blur' }
        ],
        reportNo: [
          { required: true, message: '作业单号不能为空!', trigger: 'change' },
          { validator: checkReportNo, trigger: 'blur' }
        ],
        bomId: [
          { required: true, message: '物料Bom不能为空!', trigger: 'blur' }
        ],
        scheduleBy: [
          { required: true, message: '排程人不能为空!', trigger: 'change' }
        ]
      },
      dialogVisible: false,
      materialBomList: [],
      treeProps: {
        value: 'id',
        label: 'factoryName',
        children: 'children'
      },
      detailInfo: {}
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      this.detailInfo = row
      const res = await this.$axios.get(`/pro/schedule/detail?id=${row.id}`)
      if (res.data.code === 200) {
        this.ruleForm = { ...this.ruleForm, ...res.data.data }
        this.getMaterialBom(row.materialId)
        this.dialogVisible = true
      }
    },
    getValue(val) {
      this.ruleForm.factoryId = val.id
    },
    /**
     * @description: 获取物料Bom
     * @return {*}
     */
    async getMaterialBom(id) {
      const res = await this.$axios.get(`/pro/schedule/getBom?materialId=${id}`)
      if (res.data.code === 200) {
        this.materialBomList = res.data.data
      }
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitFormSave(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post(
            '/pro/schedule/save',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.resetForm('ruleForm')
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        factoryId: '',
        reportNo: '',
        scheduleNo: '',
        scheduleBeginDate: '',
        scheduleEndDate: '',
        bomId: '',
        scheduleBy: null,
        scheduleNum: '',
        remark: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
